/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DataPenyewaanMobilPanel.java
 *
 * Created on Nov 18, 2011, 5:44:42 PM
 */
package DataMobil;

import DataMobil.Intermobil;
import Login.Login;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.Timer;

/**
 *
 * @author Piyo
 */
public class DataPengembalianMobilPanel extends javax.swing.JPanel {

    Intermobil tot;

    /** Creates new form DataPenyewaanMobilPanel */
    public DataPengembalianMobilPanel() {
        initComponents();
        GregorianCalendar gc = new GregorianCalendar();
        tglSekarangChooser.setDate(gc.getTime());
        this.tot = new Intermobil();
        NAMAOPTF.setText(Login.getLogin1().getUsername());
        ActionListener taskPerformer = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                String nol_jam = "";
                String nol_menit = "";
                String nol_detik = "";

                Date dt = new Date();

                int nilai_jam = dt.getHours();
                int nilai_menit = dt.getMinutes();
                int nilai_detik = dt.getSeconds();

                if (nilai_jam <= 9) {

                    nol_jam = "0";
                }

                if (nilai_menit <= 9) {

                    nol_menit = "0";
                }

                if (nilai_detik <= 9) {

                    nol_detik = "0";
                }

                String jam = nol_jam + Integer.toString(nilai_jam);
                String menit = nol_menit + Integer.toString(nilai_menit);
                String detik = nol_detik + Integer.toString(nilai_detik);

                JAMTF.setText("  " + jam + " : " + menit + " : " + detik + "  ");
            }
        };

        new Timer(1000, taskPerformer).start();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField6 = new javax.swing.JTextField();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        Notranstxt = new javax.swing.JLabel();
        nama = new javax.swing.JLabel();
        Tanggal = new javax.swing.JLabel();
        jam = new javax.swing.JLabel();
        JAMTF = new javax.swing.JTextField();
        NOTRANTF = new javax.swing.JTextField();
        NAMAOPTF = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        IDPenyewatxt = new javax.swing.JLabel();
        NamaPenyewatxt = new javax.swing.JLabel();
        idPenyewaTF = new javax.swing.JTextField();
        namaPenyewaTF = new javax.swing.JTextField();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        MerkTF = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        idMobilTF = new javax.swing.JTextField();
        namaMobilTF = new javax.swing.JTextField();
        jSeparator4 = new javax.swing.JSeparator();
        terlambatLabel = new javax.swing.JLabel();
        tanggalKembaliTF = new javax.swing.JTextField();
        SUBMITBUTTON = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        TotalField = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        WarnaTF = new javax.swing.JTextField();
        tanggalPinjamLabel = new javax.swing.JLabel();
        tanggalSewaTF = new javax.swing.JTextField();
        lamaKeterlambatanTF = new javax.swing.JTextField();
        tglSekarangChooser = new com.toedter.calendar.JDateChooser();
        jLabel11 = new javax.swing.JLabel();

        jTextField6.setText("jTextField6");

        jDesktopPane1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Lucida Sans Unicode", 1, 14));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Form Pengembalian Mobil");
        jLabel1.setBounds(210, 10, 220, 20);
        jDesktopPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator1.setBounds(0, 50, 600, 10);
        jDesktopPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Notranstxt.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        Notranstxt.setForeground(new java.awt.Color(255, 255, 255));
        Notranstxt.setText("No Transaksi");
        Notranstxt.setBounds(10, 60, 90, 30);
        jDesktopPane1.add(Notranstxt, javax.swing.JLayeredPane.DEFAULT_LAYER);

        nama.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        nama.setForeground(new java.awt.Color(255, 255, 255));
        nama.setText("Nama Operator");
        nama.setBounds(10, 100, 100, 30);
        jDesktopPane1.add(nama, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Tanggal.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        Tanggal.setForeground(new java.awt.Color(255, 255, 255));
        Tanggal.setText("Tanggal ");
        Tanggal.setBounds(330, 60, 110, 30);
        jDesktopPane1.add(Tanggal, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jam.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        jam.setForeground(new java.awt.Color(255, 255, 255));
        jam.setText("Jam ");
        jam.setBounds(330, 100, 140, 30);
        jDesktopPane1.add(jam, javax.swing.JLayeredPane.DEFAULT_LAYER);
        JAMTF.setBounds(430, 100, 150, 30);
        jDesktopPane1.add(JAMTF, javax.swing.JLayeredPane.DEFAULT_LAYER);

        NOTRANTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                NOTRANTFKeyReleased(evt);
            }
        });
        NOTRANTF.setBounds(140, 60, 130, 30);
        jDesktopPane1.add(NOTRANTF, javax.swing.JLayeredPane.DEFAULT_LAYER);
        NAMAOPTF.setBounds(140, 100, 130, 30);
        jDesktopPane1.add(NAMAOPTF, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator2.setBounds(0, 140, 600, 20);
        jDesktopPane1.add(jSeparator2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        IDPenyewatxt.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        IDPenyewatxt.setForeground(new java.awt.Color(255, 255, 255));
        IDPenyewatxt.setText("ID Penyewa");
        IDPenyewatxt.setBounds(10, 150, 100, 30);
        jDesktopPane1.add(IDPenyewatxt, javax.swing.JLayeredPane.DEFAULT_LAYER);

        NamaPenyewatxt.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        NamaPenyewatxt.setForeground(new java.awt.Color(255, 255, 255));
        NamaPenyewatxt.setText("Nama Penyewa");
        NamaPenyewatxt.setBounds(10, 190, 90, 30);
        jDesktopPane1.add(NamaPenyewatxt, javax.swing.JLayeredPane.DEFAULT_LAYER);
        idPenyewaTF.setBounds(140, 150, 130, 30);
        jDesktopPane1.add(idPenyewaTF, javax.swing.JLayeredPane.DEFAULT_LAYER);
        namaPenyewaTF.setBounds(140, 190, 130, 30);
        jDesktopPane1.add(namaPenyewaTF, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator3.setBounds(0, 230, 610, 20);
        jDesktopPane1.add(jSeparator3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Nama Mobil");
        jLabel3.setBounds(10, 280, 100, 30);
        jDesktopPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("ID Mobil");
        jLabel4.setBounds(10, 240, 90, 30);
        jDesktopPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);
        MerkTF.setBounds(420, 240, 160, 30);
        jDesktopPane1.add(MerkTF, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Warna Mobil");
        jLabel2.setBounds(340, 280, 80, 30);
        jDesktopPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Merk Mobil");
        jLabel5.setBounds(340, 240, 80, 30);
        jDesktopPane1.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);
        idMobilTF.setBounds(140, 240, 130, 30);
        jDesktopPane1.add(idMobilTF, javax.swing.JLayeredPane.DEFAULT_LAYER);
        namaMobilTF.setBounds(140, 280, 130, 30);
        jDesktopPane1.add(namaMobilTF, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator4.setBounds(0, 320, 610, 10);
        jDesktopPane1.add(jSeparator4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        terlambatLabel.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        terlambatLabel.setForeground(new java.awt.Color(255, 255, 255));
        terlambatLabel.setText("Lama Keterlambatan");
        terlambatLabel.setBounds(10, 420, 110, 30);
        jDesktopPane1.add(terlambatLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        tanggalKembaliTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tanggalKembaliTFKeyPressed(evt);
            }
        });
        tanggalKembaliTF.setBounds(140, 380, 130, 30);
        jDesktopPane1.add(tanggalKembaliTF, javax.swing.JLayeredPane.DEFAULT_LAYER);

        SUBMITBUTTON.setText("Submit");
        SUBMITBUTTON.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SUBMITBUTTONActionPerformed(evt);
            }
        });
        SUBMITBUTTON.setBounds(470, 450, 90, 23);
        jDesktopPane1.add(SUBMITBUTTON, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel10.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        jLabel10.setForeground(new java.awt.Color(255, 255, 255));
        jLabel10.setText("Total");
        jLabel10.setBounds(330, 340, 120, 30);
        jDesktopPane1.add(jLabel10, javax.swing.JLayeredPane.DEFAULT_LAYER);
        TotalField.setBounds(430, 340, 150, 30);
        jDesktopPane1.add(TotalField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel12.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        jLabel12.setForeground(new java.awt.Color(255, 255, 255));
        jLabel12.setText("Tanggal Kembali");
        jLabel12.setBounds(10, 380, 130, 30);
        jDesktopPane1.add(jLabel12, javax.swing.JLayeredPane.DEFAULT_LAYER);
        WarnaTF.setBounds(420, 280, 160, 30);
        jDesktopPane1.add(WarnaTF, javax.swing.JLayeredPane.DEFAULT_LAYER);

        tanggalPinjamLabel.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        tanggalPinjamLabel.setForeground(new java.awt.Color(255, 255, 255));
        tanggalPinjamLabel.setText("Tanggal Sewa");
        tanggalPinjamLabel.setBounds(10, 347, 90, 20);
        jDesktopPane1.add(tanggalPinjamLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        tanggalSewaTF.setBounds(140, 340, 130, 30);
        jDesktopPane1.add(tanggalSewaTF, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lamaKeterlambatanTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                lamaKeterlambatanTFKeyPressed(evt);
            }
        });
        lamaKeterlambatanTF.setBounds(140, 420, 130, 30);
        jDesktopPane1.add(lamaKeterlambatanTF, javax.swing.JLayeredPane.DEFAULT_LAYER);

        tglSekarangChooser.setDateFormatString("dd-MM-yyyy");
        tglSekarangChooser.setBounds(430, 60, 150, 30);
        jDesktopPane1.add(tglSekarangChooser, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel11.setForeground(new java.awt.Color(0, 51, 153));
        jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/DataMobil/hucomm5jv7-iam-blue-wallpaper.jpg"))); // NOI18N
        jLabel11.setBounds(0, 0, 600, 510);
        jDesktopPane1.add(jLabel11, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 589, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

private void NOTRANTFKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_NOTRANTFKeyReleased
    try {
        DataSewa transaksi = new DataSewa();

        String no_transaksi = NOTRANTF.getText();

        transaksi.setNotransaksi(no_transaksi);

        Intermobil interM = new Intermobil();
        interM.getBynoTransaksi(transaksi);

        String nama_operator = transaksi.getNamaOperator();
        String id_penyewa = transaksi.getId_penyewa();
        String nama_penyewa = transaksi.getNama_penyewa();
        String id_mobil = transaksi.getId_mobil();
        String nama_mobil = transaksi.getNama_mobil();
        String merk_mobil = transaksi.getMerkMobil();
        String warna_mobil=transaksi.getWarnaMobil();
        String tanggal_sewa = transaksi.getTgl_sewa();
        String tanggal_kembali = transaksi.getTgl_kembali();


        NAMAOPTF.setText(nama_operator);
        idPenyewaTF.setText(id_penyewa);
        namaPenyewaTF.setText(nama_penyewa);
        idMobilTF.setText(id_mobil);
        namaMobilTF.setText(nama_mobil);
        MerkTF.setText(merk_mobil);
        WarnaTF.setText(warna_mobil);
        tanggalSewaTF.setText(tanggal_sewa);
        tanggalKembaliTF.setText(tanggal_kembali);
    } catch (SQLException ex) {
        Logger.getLogger(DataPengembalianMobilPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_NOTRANTFKeyReleased

    private int kabisat(int a) {
        if (((a % 4 == 0) && (a % 100 != 0)) || (a % 400 == 0)) {
            return 1;
        }
        return 0;
    }
private void tanggalKembaliTFKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tanggalKembaliTFKeyPressed
    // TODO add your handling code here:
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        java.util.Calendar cal1 = Calendar.getInstance();
        java.util.Calendar cal2 = Calendar.getInstance();
        String cat[] = tanggalKembaliTF.getText().split("-");
        cal1.set(Integer.parseInt(cat[2]),
                Integer.parseInt(cat[1]),
                Integer.parseInt(cat[0]));
        cal2.set(tglSekarangChooser.getDate().getYear() + 1900,
                tglSekarangChooser.getDate().getMonth() + 1,
                tglSekarangChooser.getDate().getDate());
        long milliseconds1 = cal1.getTimeInMillis();
        long milliseconds2 = cal2.getTimeInMillis();
        long diff = milliseconds2 - milliseconds1;
        long diffDays = diff / (24 * 60 * 60 * 1000);
        if (diffDays > 0) {
            lamaKeterlambatanTF.setText("" + diffDays);
        } else {
            lamaKeterlambatanTF.setText("0");
        }
    }

}//GEN-LAST:event_tanggalKembaliTFKeyPressed

private void lamaKeterlambatanTFKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_lamaKeterlambatanTFKeyPressed
// TODO add your handling code here:
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        try {
            DataSewa sewa = new DataSewa();
            sewa.setNotransaksi(NOTRANTF.getText());
            tot.getBynotran(sewa);
            int lama = Integer.parseInt(lamaKeterlambatanTF.getText());
            Double Total = lama * sewa.getHargasewa() + sewa.getTotal();
            TotalField.setText(String.valueOf(Total));
        } catch (SQLException ex) {
            Logger.getLogger(DataPengembalianMobilPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}//GEN-LAST:event_lamaKeterlambatanTFKeyPressed

private void SUBMITBUTTONActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SUBMITBUTTONActionPerformed
    try {
        // TODO add your handling code here:
        DataMobil dat = new DataMobil();
       
        dat.setId_mobil(idMobilTF.getText());
        dat.setStatus("available");
        tot.updatestatus(dat);
        String tampil = "dd-MM-yyyy";
        SimpleDateFormat format = new SimpleDateFormat(tampil);
        String tanggalSewa =String.valueOf(format.format(tglSekarangChooser.getDate()));
        DataSewa c = new DataSewa();
        c.setNotransaksi(NOTRANTF.getText());
        c.setTgl_sewa(tanggalSewaTF.getText());
        c.setTgl_kembali(tanggalSewa);
        c.setId_penyewa(idPenyewaTF.getText());
        c.setNama_penyewa(namaPenyewaTF.getText());
        c.setId_mobil(idMobilTF.getText());
        c.setNama_mobil(namaMobilTF.getText());
        c.setMerkMobil(MerkTF.getText());
        c.setWarnaMobil(WarnaTF.getText());
        c.setTotal(Double.parseDouble(TotalField.getText()));
        tot.insertakhr(c);
        tot.deletesewa(NOTRANTF.getText());
        if ("0".equals(lamaKeterlambatanTF.getText())) {
            JOptionPane.showMessageDialog(null, "Terima Kasih");
        } else {
            JOptionPane.showMessageDialog(null, "karena terlambat " + lamaKeterlambatanTF.getText() + "hari maka anda harus membayar " + TotalField.getText());
        }
    } catch (SQLException ex) {
        Logger.getLogger(DataPengembalianMobilPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_SUBMITBUTTONActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel IDPenyewatxt;
    private javax.swing.JTextField JAMTF;
    private javax.swing.JTextField MerkTF;
    private javax.swing.JTextField NAMAOPTF;
    private javax.swing.JTextField NOTRANTF;
    private javax.swing.JLabel NamaPenyewatxt;
    private javax.swing.JLabel Notranstxt;
    private javax.swing.JButton SUBMITBUTTON;
    private javax.swing.JLabel Tanggal;
    private javax.swing.JTextField TotalField;
    private javax.swing.JTextField WarnaTF;
    private javax.swing.JTextField idMobilTF;
    private javax.swing.JTextField idPenyewaTF;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JLabel jam;
    private javax.swing.JTextField lamaKeterlambatanTF;
    private javax.swing.JLabel nama;
    private javax.swing.JTextField namaMobilTF;
    private javax.swing.JTextField namaPenyewaTF;
    private javax.swing.JTextField tanggalKembaliTF;
    private javax.swing.JLabel tanggalPinjamLabel;
    private javax.swing.JTextField tanggalSewaTF;
    private javax.swing.JLabel terlambatLabel;
    private com.toedter.calendar.JDateChooser tglSekarangChooser;
    // End of variables declaration//GEN-END:variables
}
